var QQMapWX = require('dist/js/qqmap-wx-jssdk.min.js');
var qqmapsdk = new QQMapWX({
  key: 'HLFBZ-EGZK6-MVQS6-ESMKT-T2G37-GIB2U'
});
var util = require('utils/util.js');
var G = {};
let _this;
// G.APP_LINK = 'http://192.168.1.107:8080';
G.APP_LINK = 'https://www.ulpey.com';
G.APP_LINK_SERVER = 'LepeiServer';
let list = {}, link = {};
list.commodityList = '/' + G.APP_LINK_SERVER + '/rest/product/list';
list.serchHot = '/' + G.APP_LINK_SERVER +'/rest/category/hot';
list.orderLst = '/' + G.APP_LINK_SERVER + '/rest/order/query';

link.productDetail = '/' + G.APP_LINK_SERVER +'/rest/product/getByIDNew';
link.deviceSubmit = '/' + G.APP_LINK_SERVER +'/rest/app/device/submit';
link.getUserDevice = '/' + G.APP_LINK_SERVER +'/rest/app/device/getUserDevice';
link.getCategoryByPidNew = '/' + G.APP_LINK_SERVER +'/rest/category/tree';
link.orderAapp = '/' + G.APP_LINK_SERVER +'/rest/order/app';
link.getShopCar = '/' + G.APP_LINK_SERVER +'/rest/order/car/user';
link.shopAdd = '/' + G.APP_LINK_SERVER +'/rest/order/car';
link.shopChecked = '/' + G.APP_LINK_SERVER +'/rest/order/car';
link.shopDelete = '/' + G.APP_LINK_SERVER +'/rest/order/car/item/';
link.addressList = '/' + G.APP_LINK_SERVER +'/rest/app/address/query';
link.addressDelete = '/' + G.APP_LINK_SERVER +'/rest/app/address/delete';
link.addressAdd = '/' + G.APP_LINK_SERVER +'/rest/app/address/add';
link.addressEdit = '/' + G.APP_LINK_SERVER +'/rest/app/address/update';
link.addressDef = '/' + G.APP_LINK_SERVER +'/rest/app/address/setDefault';
link.orderApp = '/' + G.APP_LINK_SERVER +'/rest/order/app';
link.orderDetail = '/' + G.APP_LINK_SERVER +'/rest/order';
link.orderPay = '/' + G.APP_LINK_SERVER +'/rest/wx/pay/app/order';
link.requireNeed = '/' + G.APP_LINK_SERVER +'/rest/handler/require/need';
link.requireYzm = '/' + G.APP_LINK_SERVER +'/rest/require/captcha';
link.oderFinished = '/' + G.APP_LINK_SERVER +'/rest/order';
link.cancelOrder = '/' + G.APP_LINK_SERVER +'/rest/order';

App({
  globalData: {
    appLink: G.APP_LINK + '/' + G.APP_LINK_SERVER + '/',
    becomeTechnician: null,
    userInfo: null,
    xieYouUserInfo: null,
    xieYouUserInfoRoot: {
      userBrandModel : null,
      userBrandsModel: [],
      brandAll: null
    },
    Maintenance: {
      serviceType: null,
      serviceTitle: null,
      userArea: {},
      brandModelId: null,
      recommendProductsList: null,//推荐保养产品
      shopCar:null,
      serviceDate:null,
      userBrandModel:null
    },
    allArea: null,
    storage: null,
    getUserInfoPop: false,
    repairmanList: [],
    telPhone: '4008926366' ,
  },
  onLaunch() {
    _this = this
    // _this.reverseGeocoder();
    // 获取地区城市
    qqmapsdk.getCityList({
      success: function (res) {
        _this.globalData.allArea = res;
      },
      fail: function (res) {
        console.log(res);
      },
      complete: function (res) {
        console.log(res);
      }
    });
  },
  // 刷新首页
  refreshIndex: function () {
    // 提交注册申请
    var _this = this;
    var code = _this.globalData.xieYouUserInfo.code
    var pages = getCurrentPages();
    var indexPage = pages[0];  //首页页面
    // 刷新首页
    indexPage.initUserBrandModelFun(code);
    indexPage.ifOder(code);
    indexPage.setData({
      userInfo: _this.globalData.xieYouUserInfo,
      userBrandModel: _this.globalData.xieYouUserInfoRoot.userBrandModel,
      userArea: _this.globalData.Maintenance.userArea,
    })
  },
  // 调用地址接口
  reverseGeocoder(callback){
    var _this = this
    qqmapsdk.reverseGeocoder({
      success: function (res) {
        // 获取地址
        var userArea = {
          latitude: res.result.ad_info.location.lat, // 经纬度
          longitude: res.result.ad_info.location.lng,
          country: res.result.ad_info.nation,        // 国家
          province: res.result.ad_info.province,     // 省
          city: res.result.ad_info.city,             // 市
          district: res.result.ad_info.district,     // 区
          adcode: res.result.ad_info.adcode,         // 代码
          formattedAddresses: res.result.formatted_addresses.recommend // 详细地址
        }
        // 判断是否选择了地址
        if (_this.globalData.Maintenance.userArea && _this.globalData.Maintenance.userArea.province){
          userArea.province = _this.globalData.Maintenance.userArea.province
        }
        if (_this.globalData.Maintenance.userArea && _this.globalData.Maintenance.userArea.city) {
          userArea.city = _this.globalData.Maintenance.userArea.city
        }
        if (_this.globalData.Maintenance.userArea && _this.globalData.Maintenance.userArea.district) {
          userArea.district = _this.globalData.Maintenance.userArea.district
        }
        if (_this.globalData.Maintenance.userArea && _this.globalData.Maintenance.userArea.adcode) {
          userArea.adcode = _this.globalData.Maintenance.userArea.adcode
        }
        _this.globalData.Maintenance.userArea = {
          latitude: userArea.latitude, // 经纬度
          longitude: userArea.longitude,
          country: userArea.country,        // 国家
          province: userArea.province,     // 省
          city: userArea.city,             // 市
          district: userArea.district,     // 区
          adcode: userArea.adcode,         // 代码
          formattedAddresses: userArea.formattedAddresses // 详细地址
        }
        callback();
      },
      fail: function (res) {
        console.log(res);
        _this.GetAddressPermissions();
      },
      complete: function (res) {
        console.log(res);
      }
    });
  },
  // 打开地址设置
  GetAddressPermissions(a , callback){
    var _this = this
    util.alertMsg('提示', '械友需要获取您的地址', function () {
      //打开选择地址  
      wx.openSetting({
        success: function (res) {
          if (res.authSetting["scope.userLocation"]) {
            _this.reverseGeocoder();
          }
          if (a) {
            callback(res.authSetting["scope.userLocation"]);
          }
        }
      })
    }, function () {
      console.log('用户点击取消')
      return;
    });
  },
  judgeGetUserInfo(callback) {
    var _this = this
    if (_this.globalData.userInfo && _this.globalData.xieYouUserInfo) {
      // app初始化绑定机型 userBrandModel
      var code = _this.globalData.xieYouUserInfo.code;
      callback(code);
    } else {
      //登录获取code
      wx.login({
        success: function (res) {
          if (res.code) {
            console.log('获取微信code',res.code);
            _this.getUserInfo(res.code,function(userCode){
                callback(userCode);
            });
          } else {
            console.log('获取用户登录态失败！' + res.errMsg)
          }
        }
      });
    }
  },
  // 获取用户信息
  getUserInfo(a, callback){
    console.log('获取用户信息0=');
    var _this = this;
    // 微信登录械友 通过code获取械友userCode
    wx.request({
      url: G.APP_LINK + '/' + G.APP_LINK_SERVER + '/rest/user/login/verify?xcxType=USER', 
      method: 'GET',
      data:{
        code:a
      },
      header: {
        'content-type': 'application/json' // 默认值
      },
      success: function (res) {
        console.log('获取用户信息1=', res);
        if (res.data.data.code) {
          console.log('获取用户信息2=', res.data.data);
          _this.globalData.xieYouUserInfo = res.data.data;
          callback(res.data.data.code);
        }else{
          // // 获取用户信息
          console.log('获取用户信息3=', res);
          var userInfo,
            openid = res.data.data.openid;
          _this.wxGetUserInfo(function(res){
            var userInfoData = res;
            wx.request({
              url: G.APP_LINK + '/' + G.APP_LINK_SERVER + '/rest/user/decode/userinfo?type=USER', //仅为示例，并非真实的接口地址
              method: 'GET',
              data: {
                encryptedData: userInfoData.encryptedData,
                openId: openid,
                iv: userInfoData.iv
              },
              header: {
                'content-type': 'application/json' // 默认值
              },
              success: function (res) {
                console.log('获取用户信息4=', res);
                _this.globalData.xieYouUserInfo = res.data.data;
              },
              fail: function (res) {
                console.log(res);
              }
            })
          });
        }
      },
      fail: function (res) {
        console.log('获取用户信息5=', res);
        console.log(res);
      }
    })
  },
  // 微信查询用户信息
  wxGetUserInfo(callback){
    var _this = this
    wx.getUserInfo({
      success: function (res) {
        callback(res)
      },
      fail: function(res){
        var pages = getCurrentPages(),
        index = pages[0];
        index.setData({
          getUserInfoPop:true
        })
        console.log(res);
      }
    });
  },
  // 根据用户信息获取电话
  getUserPhone(encryptedData, unionId, iv,callback){
    var _this = this;
    wx.request({
      url: G.APP_LINK + '/' + G.APP_LINK_SERVER + '/rest/user/decode/phone?type=USER', //仅为示例，并非真实的接口地址
      method: 'GET',
      data: {
        encryptedData: encryptedData,
        unionId: unionId,
        iv: iv
      },
      header: {
        'content-type': 'application/json' // 默认值
      },
      success: function (res) {
        if (res.data && res.statusCode == '200'){
          _this.globalData.xieYouUserInfo = res.data.data;
          callback(res.data);
        }else{
          util.alertMsg('提示', '下单需要绑定您的电话', function () {
            console.log('用户点击取消')
            return;
          }, function () {
            console.log('用户点击取消')
            return;
          });
        }
      },
      fail: function (res) {
        console.log(res);
      }
    })
  },
  //查询用户所有关联机型
  initUserBrandModel(userCode, callback){
    var url = G.APP_LINK + '/' + G.APP_LINK_SERVER + "/rest/product/userBrandModel?userCode=" + userCode;
    wx.request({
      url: url,
      method: 'GET',
      header: {
        'content-type': 'application/json' // 默认值
      },
      success: function (res) {
        if (res.data) {
          callback(res.data.data);
        } else {
          callback(null);
        }
      },
      fail: function (res) {
        callback(null);
      }
    });
  },
  //更改用户默认绑定机型
  changeUserBindBrandModel(userCode,userBrandModelId,callback){
    var url = G.APP_LINK + '/' + G.APP_LINK_SERVER + "/rest/product/changeBindBrandModel?userCode=" + userCode + "&user_brand_model_id=" + userBrandModelId;
    wx.request({
      url: url,
      method: 'GET',
      header: {
        'content-type': 'application/json' // 默认值
      },
      success: function (res) {
        if (res.data) {
          callback(res.data.data);
        } else {
          callback(null);
        }
      },
      fail: function (res) {
        callback(null);
      }
    });
  },
  // 删除绑定机型
  productDelete(uderCode, proId, callback) {
    var url = G.APP_LINK + '/' + G.APP_LINK_SERVER + '/rest/product/del/' + uderCode + '/bm/' + proId;
    wx.request({
      url: url,
      method: 'DELETE',
      header: {
        'content-type': 'application/json' // 默认值
      },
      success: function (res) {
        console.log(res);
        if (res.data) {
          callback(res.data);
        } else {
          callback(null);
        }
      },
      fail: function (res) {
        callback(null);
      }
    });
  },
  //获取所有品牌
  getAllBrand(callback){
    var url = G.APP_LINK + '/' + G.APP_LINK_SERVER + '/rest/brand/top-all';
    wx.request({
      url: url,
      method: 'GET',
      header: {
        'content-type': 'application/json' // 默认值
      },
      success: function (res) {
        if (res.data) {
          callback(res.data.data);
        } else {
          callback(null);
        }
      },
      fail: function (res) {
        callback(null);
      }
    });
  },
  //获取型号
  getModelByBrand(id, callback) {
    var url = G.APP_LINK + '/' + G.APP_LINK_SERVER + '/rest/brand/' + id + '/brandModel';
    wx.request({
      url: url,
      method: 'GET',
      header: {
        'content-type': 'application/json' // 默认值
      },
      success: function (res) {
        if (res.data) {
          callback(res.data.data);
        } else {
          callback(null);
        }
      },
      fail: function (res) {
        callback(null);
      }
    });
  },
  // 添加机型
  addBrandModel(userCode, brandModelId, workHour, callback) {
    var url = G.APP_LINK + '/' + G.APP_LINK_SERVER + '/rest/product/addBrandModel?userCode=' + userCode + '&brand_model_id=' + brandModelId + '&workHour=' + workHour;
    wx.request({
      url: url,
      method: 'GET',
      header: {
        'content-type': 'application/json' // 默认值
      },
      success: function (res) {
        if (res.data) {
          callback(res.data);
        } else {
          callback(null);
        }
      },
      fail: function (res) {
        callback(null);
      }
    });
  },
  // 获取用户订单
  getRepairmanReparing(userCode, callback) {
    var url = G.APP_LINK + '/' + G.APP_LINK_SERVER + "/rest/repairOrder/queryWorkingOrdersByCode/?userCode=" + userCode;
    wx.request({
      url: url,
      method: 'GET',
      header: {
        'content-type': 'application/json' // 默认值
      },
      success: function (res) {
        if (res.data) {
          callback(res.data.data);
        } else {
          callback(null);
        }
      },
      fail: function (res) {
        callback(null);
      }
    });
  },
  // 获取推荐产品
  getRecommendedProductsList(payload, callback, categoryNameId){
    // var url = G.APP_LINK + '/' + G.APP_LINK_SERVER + "/rest/repairOrder/getRecommendProducts?";
    var url = G.APP_LINK + '/' + G.APP_LINK_SERVER + "/rest/repairOrder/getRecommendProductsNew?";
    // var url = G.APP_LINK + '/' + G.APP_LINK_SERVER + url;
    if (categoryNameId != null){
      url += 'serviceType=' + categoryNameId
    }else if (payload.serviceType) {
      url += 'serviceType=' + payload.serviceType;
    }
    if (payload.brandModelId) {
      url += '&brandModelId=' + payload.brandModelId;
    }
    if (payload.userArea.adcode) {
      url += '&areaCode=' + payload.userArea.adcode;
    }
    wx.request({
      url: url,
      method: 'GET',
      header: {
        'content-type': 'application/json' // 默认值
      },
      success: function (res) {
        if (res.data.status == 'error') {
          // 取消加载中
          wx.hideLoading();
          util.alertMsg('提示', res.data.message, function () {
            wx.navigateBack({
              delta: 1
            })
            return;
          }, function () {
            wx.navigateBack({
              delta: 1
            })
            return;
          });
          //callback(null);
          
        } else {
          callback(res.data.data);
          
          
        }
      },
      fail: function (res) {
        callback(null);
      }
    });
  },
  //完善 上传维修工服务
  applyService(payload, callback){
    var url = G.APP_LINK + '/' + G.APP_LINK_SERVER + "/rest/repairOrder/save"; 
    var data = {
      latitude: payload.latitude,// 经度
      longitude: payload.longitude, //维度
      country: payload.country,//国家
      province: payload.province,  //省
      city: payload.city,//城市
      area: payload.area, //district 地区
      detailAddress: payload.detailAddress, //详细地址
      areaCode: payload.areaCode,

      userCode: payload.userCode,//  userCode
      contact: payload.contact,  //contact联系人
      contactPhone: payload.contactPhone, //contactPhone 联系电话
      repairmanCode: payload.repairmanCode ? repairmanCode: '',

      serviceType: payload.serviceType, //服务类型
      categoryNameId: payload.categoryNameId ? payload.categoryNameId:'',// 故障类型
      description: payload.description ? payload.description : '', //description 描述
      serviceDate: payload.serviceDate,//serviceDate 服务日期
      brandId: payload.brandId,
      brandModelId: payload.brandModelId,
      brandModelName: payload.brandModelName,
      shopCar: payload.shopCar
    };
    if (payload.servicePlaceType == 0 ){
      data.servicePlaceType = payload.servicePlaceType
    }
    if (payload.serviceTimeBegin){
      data.serviceTimeBegin = payload.serviceTimeBegin // 保养开始时间
    }
    if (payload.serviceTimeEnd) {
      data.serviceTimeEnd = payload.serviceTimeEnd  // 保养结束时间
    }
    wx.request({
      url: url,
      method: 'POST',
      data:data,
      header: {
        'content-type': 'application/json' // 默认值
      },
      success: function (res) {
        if (res.data) {
          callback(res.data);
        } else {
          callback(null);
        }
      },
      fail: function (res) {
        callback(null);
      }
    });
  },
  // 查询保养订单所属采集商列表
  getRecommendedCollectorsList(data, callback){
    var url = G.APP_LINK + '/' + G.APP_LINK_SERVER +"/rest/repairOrder/getProductsByCategory?limit=200&page=1";
    if (data.merchantCode) {
      url = url + '&merchantCode=' + data.merchantCode;
    }
    if (data.categoryId) {
      url = url + '&categoryId=' + data.categoryId;
    }
    if (data.parentCategoryId) {
      url = url + '&parentCategoryId=' + data.parentCategoryId;
    }
    // if (data.productBrandId){
    //   url = url + '&productBrandId=' + data.productBrandId;
    // }
    if (data.brandModelId){
      url = url + '&brandModelId=' + data.brandModelId;
    }
    wx.request({
      url: url,
      method: 'GET',
      header: {
        'content-type': 'application/json' // 默认值
      },
      success: function (res) {
        if (res.data) {
          callback(res.data);
        } else {
          callback(null);
        }
      },
      fail: function (res) {
        callback(null);
      }
    });
  },
  // 查询保养订单所属采集商
  getRecommendedCollectors(areaCode, callback){
    var url = G.APP_LINK + '/' + G.APP_LINK_SERVER + "/rest/repairOrder/getMerchantBrands?";
    if (areaCode) {
      url = url + 'areaCode=' + areaCode;
    }
    wx.request({
      url: url,
      method: 'GET',
      header: {
        'content-type': 'application/json' // 默认值
      },
      success: function (res) {
        if (res.data) {
          callback(res.data.data);
        } else {
          callback(null);
        }
      },
      fail: function (res) {
        callback(null);
      }
    });
  },
  // 订单状态轴
  mtQueryOrderStatus(id, callback){
    var url = G.APP_LINK + '/' + G.APP_LINK_SERVER + '/rest/repairOrder/' + id + '/state/axis';
    wx.request({
      url: url,
      method: 'GET',
      header: {
        'content-type': 'application/json' // 默认值
      },
      success: function (res) {
        if (res.data) {
          callback(res.data);
        } else {
          callback(null);
        }
      },
      fail: function (res) {
        callback(null);
      }
    });
  },
  // 查询订单详情
  querymtNewOrderDetail(id, callback){
    var url = G.APP_LINK + '/' + G.APP_LINK_SERVER + "/rest/repairOrder/queryRepairmanOrdersById/" + id;
    wx.request({
      url: url,
      method: 'GET',
      header: {
        'content-type': 'application/json' // 默认值
      },
      success: function (res) {
        if (res.data) {
          callback(res.data);
        } else {
          callback(null);
        }
      },
      fail: function (res) {
        callback(null);
      }
    });
  },
  // 获取订单商品
  orderClearCarQueryProduct(id, callback){
    var url = G.APP_LINK + '/' + G.APP_LINK_SERVER + "/rest/repairOrder/queryRepairmanOrdersProducts/" + id;
    wx.request({
      url: url,
      method: 'GET',
      header: {
        'content-type': 'application/json' // 默认值
      },
      success: function (res) {
        if (res.data) {
          callback(res.data.data);
        } else {
          callback(null);
        }
      },
      fail: function (res) {
        callback(null);
      }
    });
  },
  //确认 用户/维修工操作订单 切换状态
  mtOperationSwicthStatus(payload, callback){
    var url = G.APP_LINK + '/' + G.APP_LINK_SERVER + "/rest/repairOrder/status/modify";
    var data = {}
    if (payload.id){
      data.id = payload.id;
    }
    if (payload.statusId){
      data.status = payload.statusId;
    }
    if (payload.remark) {
      data.remark = payload.remark;
    }
    if (payload.rating) {
      data.rating = payload.rating;
    }
    if (payload.commentText) {
      data.commentText = payload.commentText;
    }
    if (payload.selfHelp){
      data.selfHelp = 'true'
    }
    if (payload.formId){
      data.formId = payload.formId
    }
    payload.payMode ? data.payMode = payload.payMode:'';
    wx.request({
      url: url,
      method: 'PUT',
      data: data,
      header: {
        'content-type': 'application/json' // 默认值
      },
      success: function (res) {
        if (res.data) {
          callback(res.data);
        } else {
          callback(null);
        }
      },
      fail: function (res) {
        callback(null);
      }
    });
  },
  //查询用户正在进行的订单
  getUserUnfinishReparing(userCode, callback){
    var url = G.APP_LINK + '/' + G.APP_LINK_SERVER + "/rest/repairOrder/repairingOrder?page=1&limit=20";
    
    if (userCode) {
      url += "&userCode=" + userCode;
    }
    wx.request({
      url: url,
      method: 'GET',
      header: {
        'content-type': 'application/json' // 默认值
      },
      success: function (res) {
        if (res.data) {
          callback(res.data);
        } else {
          callback(null);
        }
      },
      fail: function (res) {
        callback(null);
      }
    });
  },
  // 查询某个步骤的订单
  getRepairmanByStatus(param, callback){
    var url = G.APP_LINK + '/' + G.APP_LINK_SERVER + "/rest/repairOrder/queryRepairmanOrders/";
    if (param.page) {
      url += "?page=" + param.page;
    }
    if (param.limit) {
      url += "&limit=" + param.limit;
    }
    if (param.userCode) {
      url += "&userCode=" + param.userCode;
    }
    if (param.search) {
      url += "&search=" + param.search;
    }
    if (param.statusId) {
      url += "&statusId=" + param.statusId;
    }
    wx.request({
      url: url,
      method: 'GET',
      header: {
        'content-type': 'application/json' // 默认值
      },
      success: function (res) {
        if (res.data) {
          callback(res.data);
        } else {
          callback(null);
        }
      },
      fail: function (res) {
        callback(null);
      }
    });
  },
  getUserOrderList(param,callback){
    var url = G.APP_LINK + '/' + G.APP_LINK_SERVER + "/rest/repairOrder/userOrderList";
    // if (param.page) {
    //   url += "?page=" + param.page;
    // }
    // if (param.limit) {
    //   url += "&limit=" + param.limit;
    // }
    // if (param.userCode) {
    //   url += "&userCode=" + param.userCode;
    // }
    // if (param.orderStatus) {
    //   url += "&orderStatus=" + param.orderStatus;
    // }
    // if (param.payStatus) {
    //   url += "&payStatus=" + param.payStatus;
    // }
    var data={};
    if(param.page) {
      data.page=param.page;
    }
    if (param.limit) {
      data.limit =param.limit;
    }
    if (param.userCode) {
      data.userCode =param.userCode;
    }
    if (param.orderStatus) {
      data.status =param.orderStatus;
    }
    if (param.payStatus) {
      data.payStatus =param.payStatus;
    }
    wx.request({
      url: url,
      method: 'GET',
      data: data,
      header: {
        'content-type': 'application/json' // 默认值
      },
      success: function (res) {
        if (res.data) {
          callback(res.data);
        } else {
          callback(null);
        }
      },
      fail: function (res) {
        callback(null);
      }
    });
  },
  // 匹配维修师
  mtMatching(payload, page, limit, callback){
    var url = G.APP_LINK + '/' + G.APP_LINK_SERVER + "/rest/repairman/match" + '?page=' + page + '&limit=' + limit;
    if (payload.id) {
      url += "&repairOrderId=" + payload.id;
    }
    if (payload.id) {
      url += "&serviceType=" + payload.serviceType;
    }
    if (payload.id) {
      url += "&date=" + payload.date;
    }
    if (payload.id) {
      url += "&province=" + payload.province;
    }
    if (payload.id) {
      url += "&city=" + payload.city;
    }
    if (payload.id) {
      url += "&district=" + payload.district;
    }
    if (payload.id) {
      url += "&brand=" + payload.brand;
    }
    wx.request({
      url: url,
      method: 'GET',
      header: {
        'content-type': 'application/json' // 默认值
      },
      success: function (res) {
        if (res.data) {
          callback(res.data);
        } else {
          callback(null);
        }
      },
      fail: function (res) {
        callback(null);
      }
    });
  },
  // 选择维修师
  mtChooseMaintenance(payload, callback,data){
    // var url = G.APP_LINK + '/' + G.APP_LINK_SERVER + "/rest/repairOrder/" + payload.id + "/appoint/" + payload.repairmanCode;
    var url = G.APP_LINK + '/' + G.APP_LINK_SERVER + "/rest/repairOrder/" + payload.id + "/confirm/" + payload.repairmanCode;
    wx.request({
      url: url,
      data: data,
      method: 'PUT',
      header: {
        'content-type': 'application/json' // 默认值
      },
      success: function (res) {
        if (res.data) {
          callback(res.data.data);
        } else {
          callback(null);
        }
      },
      fail: function (res) {
        callback(null);
      }
    });
  },
  // 查看维修师 2018-6-7
  checkRepairMan(id,callback){
    var url = G.APP_LINK + '/' + G.APP_LINK_SERVER + "/rest/repairman/appoint/orders/"+id;
    wx.request({
      url: url,
      method: 'GET',
      header: {
        'content-type': 'application/json' // 默认值
      },
      success: function (res) {
        if (res.data) {
          callback(res.data.data);
        } else {
          callback(null);
        }
      },
      fail: function (res) {
        callback(null);
      }
    });
  },
  // 维修师评价列表 2018-6-7
  evaluateRepairMan(id, page, limit, callback) {
    var url = G.APP_LINK + '/' + G.APP_LINK_SERVER + "/rest/repairman/comment/list";
    var data = {}
    if(id){
      data.repairmanCode = id
    }
    if (page){
      data.page = page
    }
    if (limit) {
      data.limit = limit
    }
    wx.request({
      url: url,
      data: data,
      method: 'GET',
      header: {
        'content-type': 'application/json' // 默认值
      },
      success: function (res) {
        if (res.data) {
          callback(res.data.data);
        } else {
          callback(null);
        }
      },
      fail: function (res) {
        callback(null);
      }
    });
  },
  // 核算订单
  placeRepairOrder(payload,callback){
    var _this = this,
      data = {
        userCode: _this.globalData.xieYouUserInfo.code,
        way: "微信支付"
      };
    var url = G.APP_LINK + '/' + G.APP_LINK_SERVER + "/rest/repairOrder/order/app";
    if (payload.benefit){
      if (payload.benefit.chooseCouponId){
        data.couponId = payload.benefit.chooseCouponId
      }
    }
    if (payload.id) {
      data.id = payload.id
    }
    if (payload.mark) {
      data.mark = payload.mark
    }
    if (payload.repairFund){
      data.repairFundDiscount = payload.repairFund
    }
    if (payload.formId) {
      data.formId = payload.formId
    }
    
    wx.request({
      url: url,
      method: 'POST',
      data: data,
      header: {
        'content-type': 'application/json' // 默认值
      },
      success: function (res) {
        if (res.data) {
          callback(res);
        } else {
          callback(res);
        }
      },
      fail: function (res) {
        callback(null);
      }
    });
  },
  // 支付
  repairOrderPay(id,callback){
    var url = G.APP_LINK + '/' + G.APP_LINK_SERVER + "/rest/repairOrder/app/order/" + id +"?appKey=miniprogram";
    wx.request({
      url: url,
      method: 'GET',
      data: {
        appKey: 'miniprogram'
      },
      header: {
        'content-type': 'application/json' // 默认值
      },
      success: function (res) {
        if (res.data) {
          callback(res.data.data);
        } else {
          callback(null);
        }
      },
      fail: function (res) {
        callback(null);
      }
    });
  },
  // 检查是否申请了维修师
  accountAll(userCode,callback){
    var url = G.APP_LINK + '/' + G.APP_LINK_SERVER + "/rest/repairman/getByUserCode?userCode=" + userCode;
    wx.request({
      url: url,
      method: 'GET',
      header: {
        'content-type': 'application/json' // 默认值
      },
      success: function (res) {
        if (res.data) {
          callback(res.data.data);
        } else {
          callback(null);
        }
      },
      fail: function (res) {
        callback(null);
      }
    });
  },
  // 上传图片
  uploadImg: function (payload, callback) {
    //http://ulpey.com/LepeiServer/rest/image/upload
    var url = G.APP_LINK + '/' + G.APP_LINK_SERVER + "/rest/image/upload";
    var data = {
      // images: payload.images,
      type: payload.type,
      thumb: payload.thumb
    }
    wx.uploadFile({
      url: url,
      filePath: payload.images,
      name: 'images',
      formData: data,
      header: {
      "Content-Type": "multipart/form-data"  
      },  
      success: function (res) {
          console.log('success',res)
        if (res.data) {
          if (typeof (res.data) == 'string'){
            var rs = JSON.parse(res.data);
            callback(rs.data[0]);
          }else{
            callback(res.data);  
          }
        } else {
          callback(null);
        }
      },
      fail: function (res) {
        console.log('fail', res)
        callback(null);
      }
    });
  },
  // 获取服务类型
  getServiceType(callback) {
    var url = G.APP_LINK + '/' + G.APP_LINK_SERVER + "/rest/repairCategory/all";
    wx.request({
      url: url,
      method: 'GET',
      header: {
        'content-type': 'application/json' // 默认值
      },
      success: function (res) {
        if (res.data) {
          callback(res.data.data);
        } else {
          callback(null);
        }
      },
      fail: function (res) {
        callback(null);
      }
    });
  },
  //品牌列表
  brandCategory:function(callback){
    var url = G.APP_LINK + '/' + G.APP_LINK_SERVER + "/rest/brandCategory/00/brand";
    wx.request({
      url: url,
      method: 'GET',
      header: {
        'content-type': 'application/json' // 默认值
      },
      success: function (res) {
        if (res.data) {
          callback(res.data.data);
        } else {
          callback(null);
        }
      },
      fail: function (res) {
        callback(null);
      }
    });
  },
  //维修师编辑个人资料 （注册维修师）
  repairmanCompleteInfo: function (payload, callback) {
    var url = G.APP_LINK + '/' + G.APP_LINK_SERVER + "/rest/repairman/completeInfo";
    wx.request({
      url: url,
      data: payload,
      method: 'POST',
      header: {
        'content-type': 'application/json' // 默认值
      },
      success: function (res) {
        if (res.data) {
          callback(res.data.data);
        } else {
          callback(null);
        }
      },
      fail: function (res) {
        callback(null);
      }
    });
  },
  //查询维修基金记录
  getFundMoneyRecord: function (payload, callback){
    var url = G.APP_LINK + '/' + G.APP_LINK_SERVER + "/rest/app/user/fundBills";
    var data=''
    if (payload.userCode){
      url = url + '?userCode=' + payload.userCode
    }
    if (payload.type) {
      url = url + '&type=' + payload.type
    }
    if (payload.date) {
      url = url + '&date=' + payload.date
    }
    wx.request({
      url: url,
      method: 'GET',
      header: {
        'content-type': 'application/json' // 默认值
      },
      success: function (res) {
        if (res.data) {
          callback(res.data.data);
        } else {
          callback(null);
        }
      },
      fail: function (res) {
        callback(null);
      }
    });
  },
  // 查询用户维修基金余额
  getFundMoney: function (userCode, callback) {
    var url = G.APP_LINK + '/' + G.APP_LINK_SERVER + "/rest/app/user/fundBalance?userCode=" + userCode;
    wx.request({
      url: url,
      method: 'GET',
      header: {
        'content-type': 'application/json' // 默认值
      },
      success: function (res) {
        if (res.data) {
          callback(res.data.data);
        } else {
          callback(null);
        }
      },
      fail: function (res) {
        callback(null);
      }
    });
  },
  //查询某维修订单的维修基金奖励接口
  getOrdersRepairFund: function (repairOrderId,callback){
    var url = G.APP_LINK + '/' + G.APP_LINK_SERVER + "/rest/repairOrder/getOrdersRepairFund?repairOrderId=" + repairOrderId;
    wx.request({
      url: url,
      method: 'GET',
      header: {
        'content-type': 'application/json' // 默认值
      },
      success: function (res) {
        if (res.data) {
          callback(res.data.data);
        } else {
          callback(null);
        }
      },
      fail: function (res) {
        callback(null);
      }
    });
  },
  repairOrderModify: function (payload, callback){
    var url = G.APP_LINK + '/' + G.APP_LINK_SERVER + "/rest/repairOrder/modify";
    var data = {}
    if (payload.id){
      data.id = payload.id
    }
    if (payload.contact) {
      data.contact = payload.contact
    }
    if (payload.contactPhone) {
      data.contactPhone = payload.contactPhone
    }
    wx.request({
      url: url,
      method: 'PUT',
      data: data,
      header: {
        'content-type': 'application/json' // 默认值
      },
      success: function (res) {
        if (res.data) {
          callback(res.data);
        } else {
          callback(null);
        }
      },
      fail: function (res) {
        callback(null);
      }
    });
  },
  // 继续等待
  keepWaiting:function(id, callback){
    var url = G.APP_LINK + '/' + G.APP_LINK_SERVER + "/rest/repairOrder/timeout/continue/wait/" + id;
    wx.request({
      url: url,
      method: 'PUT',
      header: {
        'content-type': 'application/json' // 默认值
      },
      success: function (res) {
        if (res.data) {
          callback(res.data);
        } else {
          callback(null);
        }
      },
      fail: function (res) {
        callback(null);
      }
    });
  },
  myCoupons: function (payload, callback){
    var url = G.APP_LINK + '/' + G.APP_LINK_SERVER + "/rest/app/user/coupons";
    var data = {};
    if (payload.userCode){
      data.userCode = payload.userCode;
    }
    if (payload.couponStatus) {
      data.couponStatus = payload.couponStatus;
    }
    wx.request({
      url: url,
      method: 'GET',
      data:data,
      header: {
        'content-type': 'application/json' // 默认值
      },
      success: function (res) {
        if (res.data) {
          callback(res.data.data);
        } else {
          callback(null);
        }
      },
      fail: function (res) {
        callback(null);
      }
    });
  },
  // 2018-9-26
  priceDetail: function (payload, callback){
    //  repairOrder / price / detail
    var url = G.APP_LINK + '/' + G.APP_LINK_SERVER + "/rest/repairOrder/price/detail";
    var data = {
      latitude: payload.latitude,// 经度
      longitude: payload.longitude, //维度
      country: payload.country,//国家
      province: payload.province,  //省
      city: payload.city,//城市
      area: payload.area, //district 地区
      detailAddress: payload.detailAddress, //详细地址
      areaCode: payload.areaCode,

      userCode: payload.userCode,//  userCode
      contact: payload.contact,  //contact联系人
      contactPhone: payload.contactPhone, //contactPhone 联系电话
      repairmanCode: payload.repairmanCode ? repairmanCode : '',

      serviceType: payload.serviceType, //服务类型
      categoryNameId: payload.categoryNameId ? payload.categoryNameId : '',// 故障类型
      description: payload.description ? payload.description : '', //description 描述
      serviceDate: payload.serviceDate,//serviceDate 服务日期
      brandId: payload.brandId,
      brandModelId: payload.brandModelId,
      shopCar: payload.shopCar
    };
    if (payload.servicePlaceType == 0) {
      data.servicePlaceType = payload.servicePlaceType
    }
    if (payload.serviceTimeBegin) {
      data.serviceTimeBegin = payload.serviceTimeBegin // 保养开始时间
    }
    if (payload.serviceTimeEnd) {
      data.serviceTimeEnd = payload.serviceTimeEnd  // 保养结束时间
    }

    wx.request({
      url: url,
      method: 'POST',
      data: data,
      header: {
        'content-type': 'application/json' // 默认值
      },
      success: function (res) {
        if (res.data) {
          callback(res);
        } else {
          callback(res);
        }
      },
      fail: function (res) {
        callback(null);
      }
    });
  },
  // 获取 信息
  getMassage: function (data, id, url, callback, method) {
    var url = G.APP_LINK + link[url];
    id ? url = url + '/' + id : '';
    wx.request({
      url: url,
      data: data ? data : {},
      method: method ? method : 'POST',
      header: {
        'content-type': 'application/json' // 默认值
      },
      success: function (res) {
        if (res.data) {
          callback(res.data);
        }
      },
      fail: function (res) {
        //TODO
        callback(false);
      }
    });
  },
  getList: function (page, limit, info, url, callback, method) {
    var url = G.APP_LINK + list[url];
    var data = info ? info : {};
    data.page = page;
    data.limit = limit;
    wx.request({
      url: url,
      data: data,
      method: method ? method : 'GET',
      header: {
        'content-type': 'application/json' // 默认值
      },
      success: function (res) {
        if (res.data) {
          callback(res.data);
        }
      },
      fail: function (res) {
        //TODO
        callback(false);
      }
    });
  },
  // 返回 跳转页面
  goBack: function (e, buttonClicked, _this) {
    var delta = e.currentTarget.dataset.delta;
    if (!buttonClicked) {
      util.buttonClicked(_this);
      wx.navigateBack({
        delta: delta
      })
    }
  },
  goNavTo: function (e, buttonClicked, _this) {
    let url = e.currentTarget.dataset.url;
    let id = e.currentTarget.dataset.id;
    let salonId = e.currentTarget.dataset.salonId;
    id ? url = url + '?id=' + id : url;
    salonId ? url = url + '?salonId=' + salonId : url;
    if (!buttonClicked) {
      util.buttonClicked(_this);
      wx.navigateTo({
        url: url,
      })
    }
  },
  /*
  * 2018-11-5
  */
  // 拨打客服电话
  customerServiceTell: function () {
    var telPhone = _this.globalData.telPhone
    wx.makePhoneCall({
      phoneNumber: telPhone, //仅为示例，并非真实的电话号码
      success: function (res) {
        console.log(res, "success")
      },
      fail: function (res) {
        console.log(res, "fail")
      }
    })
  },
  // 开关
  toggleSwitch: function (e,that) {
    let name = e.currentTarget.dataset.name;
    let value = !that.data[name]
    that.setData({
      [name]: value
    })
  },
})